package example;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.equivalence.client.UpdateClient;

/**
 * asks the server to update the contents of the data directory 
 * while ignoring any changes made to data/ignore.txt
 * @author gcconner
 *
 */
public class ExampleClient {
	private UpdateClient client;
	
	public static void main(String[] args) {
		ExampleClient exc = new ExampleClient();
		
		// perform an update in a thread
		Thread thread = new Thread(exc.client);
		thread.start();	
		
		try {
			char input = (char)System.in.read();
			
			//if the transfewr has not finished and the client presses c, cancel the transfer
			if (input=='c' && thread.isAlive()) {
				exc.client.cancel();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public ExampleClient() {
		//look for a server on localhost, port 1984
		client = new UpdateClient("localhost", 1984);
		
		client.addDirectory("data");//perform versioning on the "data" dir
									//relative to the applicaiton
		
		//ignore any files in data called ignore.txt
		//the ignore feature is very simplistive, no wildcard matching etc.
		//also the name has to be an exact match i.e a file ignore.txt in data/files/ignore.txt
		//would not be ignored if the ignored file was data/ignore.txt
		client.addExcludeFile("data\\ignore.txt");
		
		//add a listener to output when the transfer is complete
		client.addCompletionListener(new completeEvent());
		
		//add a listener to keep track of the progress of the transfer
		client.addProgressListener(new progressEvent());
		
		//add a listener to keep track of any transfer events that take place
		client.addTransferEventListener(new transferEvent());
	}
	
	private class completeEvent implements ActionListener {
		/**
		 * the actioncommand will be one of complete, noupdate. or error
		 * the source object corresponds to a description of the final state
		 * of the transfer 
		 */
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("complete")) {
				System.out.println("COMPLETED "+(String)e.getSource());
			}
			else if (e.getActionCommand().equals("noupdate")) {
				System.out.println("NO UPDATE PERFORMED "+(String)e.getSource());				
			}
			else if (e.getActionCommand().equals("error")) {
				System.out.println("ENCOUNTERED ERROR "+(String)e.getSource());				
			}
		}
		
	}
	
	private class progressEvent implements ActionListener {

		/**
		 * the source object will contain a float in the region 0-1 corresponding
		 * to the progress
		 */
		public void actionPerformed(ActionEvent e) {
			System.out.println("progress: "+(((Float)e.getSource())*100));
			
		}	
	}
	
	private class transferEvent implements ActionListener {

		/**
		 * the source object will contain a string describing the latest transfer event
		 */
		public void actionPerformed(ActionEvent e) {
			System.out.println("event occurred: "+(String)e.getSource());
			
		}	
		
	}
}
